<?php

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\FinanceBankServices;
use App\Lib\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class FinanceBankController extends Controller
{
    protected $request;
    protected $service;
    protected $params;
    protected $smsPrefix = 'sms_';

    public function __construct(FinanceBankServices $service, Request $request)
    {
        $this->request = $request;
        $this->service = $service;

        init_redis_session();
        $this->params = $this->request->post();
        write_log('financeBank', '接收参数', $this->params);
    }

    public function bankNameList()
    {
        $rest = $this->service->bankNameList();
        Response::returnResult($rest['code'], $rest['data']);
    }

    /**
     * 验证码校验
     * */
    protected function checkSmsCode($phone, $smsCode)
    {
        $checkSmsCode = Redis::get($this->smsPrefix . $phone);

        if (empty($checkSmsCode) || ($checkSmsCode != $smsCode)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 绑定银行卡
     * */
    public function bindBank()
    {
        $code = Hint::SUCCESS;
        $data = [];

        do {
            /*$checkSmsCode = $this->checkSmsCode($this->params['phone'], $this->params['sms_code']);
            if (!$checkSmsCode) {
                $code = Hint::SMS_CODE_ERROR;
                break;
            }*/

            $rest = $this->service->bindBank($this->params);
            $code = $rest['code'];
            $data = $rest['data'];
        } while (false);

        write_log('financeBank', 'bindBank-绑定银行卡 code:' . $code, $data);

        $msg = empty($data['error_msg']) ? "" : $data['error_msg'];
        Response::returnResult($code, $data, $msg);
    }

    /**
     * 修改银行卡
     * */
    public function editBank()
    {
        $rest = $this->service->editBank($this->params);

        write_log('financeBank', 'editBank-修改银行卡', $rest);

        $msg = empty($rest['data']['error_msg']) ? "" : $rest['data']['error_msg'];
        Response::returnResult($rest['code'], $rest['data'], $msg);
    }

    /**
     * 删除银行卡
     * */
    public function removeBank()
    {
        $rest = $this->service->removeBank($this->params);

        write_log('financeBank', 'removeBank-删除银行卡', $rest);

        $msg = empty($rest['data']['error_msg']) ? "" : $rest['data']['error_msg'];
        Response::returnResult($rest['code'], $rest['data'], $msg);
    }

    /**
     * 查询用户银行卡
     * */
    public function getBankByUser()
    {
        $rest = $this->service->getBankByUser($this->params);

        write_log('financeBank', 'getBankByUser-查询用户银行卡', $rest);

        $msg = empty($rest['data']['error_msg']) ? "" : $rest['data']['error_msg'];
        Response::returnResult($rest['code'], $rest['data'], $msg);
    }
}